/**
 * Created by Jackie.Chen on 2017/5/26.
 */
'use strict'

var modelTemplete = require('html!./model.html');
require('./model.less');
module.exports = {
  template: modelTemplete,
  data:function(){
    return{
      modelData:[],
      modelName:'',
      selected1:'',
      selected0:'',
      isShow:false,
      userId:localStorage.getItem('userId')
    }
  },
  beforeRouteEnter:function(to, from, next){
    if(!localStorage.getItem("userId")){
      layer.confirm('请先登陆', {
        btn: ['确定'] //按钮
      }, function(index){
        layer.close(index);
        next({ path: '/login' })
      });
    }else{
      next();
    }
  },
  methods:{
    search:function(){
      var me = this;
      this.$http.get('/eyeModelController/getModelList.do?userId='+me.userId+'&modelName='+me.modelName+'&industryId='+me.selected0+'&sceneId='+me.selected1)
        .then(function(res) {
          //加动态id
          if(res.data.status === 'OK'){
            if(res.data.result.list){
              for(var i = 0;i<res.data.result.list.length;i++){
                res.data.result.list[i].demoId = 'commonDemo'+i;
              }
            }
            console.log(res.data.result.list);
            this.modelData = res.data.result.list;
          }else{
            layer.msg('系统繁忙，请稍后再试');
          }
        }.bind(this));
    },
    getAllModel:function(){
      var me = this;
      var layLoad = layer.load(2, {shade: 0.1, area: ['32px', '64px']});
      this.$http.get('/eyeModelController/getModelList.do?userId='+me.userId)
        .then(function(res) {
          //加动态id
          if(res.data.status === 'OK'){
            me.isShow = true;
            if(res.data.result.list){
              for(var i = 0;i<res.data.result.list.length;i++){
                res.data.result.list[i].demoId = 'commonDemo'+i;
              }
            }
            console.log(res.data.result.list);
            this.modelData = res.data.result.list;
            layer.close(layLoad);
          }else{
            layer.close(layLoad);
            layer.msg('系统繁忙，请稍后再试');
          }
        }.bind(this));
    },
    initCommonModel:function(){
      var me = this;
      var dataTime0=[];
      var data0=[];
      var name0,dataDemo0,option0;
      for(var i= 0;i<me.modelData.length;i++){
        dataTime0 = [];
        data0 = [];
        name0 = null;
        dataDemo0 = null;
        option0 = null;
        dataTime0.push(me.modelData[i].create_time);
        data0.push(me.modelData[i].set_num);
        name0 = me.modelData[i].scene_name;
        dataDemo0 = echarts.init(document.getElementById("commonDemo"+i));
        option0 = {
          tooltip : {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
              label: {
                backgroundColor: '#6a7985'
              }
            }
          },
          grid: {
            top:'10%',
            right: '10%',
            bottom: '5%',
            left:'5%',
            containLabel: true
          },
          xAxis : [
            {
              type : 'category',
              boundaryGap : false,
              data : dataTime0
            }
          ],
          yAxis : [
            {
              type : 'value',
              splitNumber:'3'
            }
          ],
          series : [
            {
              name:name0,
              type:'line',
              stack: '总量',
              areaStyle: {normal: {
                color:'#e6eef1'
              }},
              data:data0
            }
          ]
        };
        dataDemo0.setOption(option0);
      }
    }
  },
  created:function(){
      this.getAllModel();
  },
  updated:function(){
      this.initCommonModel();
  }
};
